package app.cn.com.school.activity

import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import app.cn.com.school.R
import app.cn.com.school.api.AppRequest
import app.cn.com.school.api.bean.response.TestResponse
import app.cn.com.school.databinding.ActivityPwdBinding
import app.cn.com.school.tools.Constants
import app.cn.com.school.tools.CountDownTimerUtils
import app.cn.com.watering.api.ApiUtill

class PwdActivity : BaseActivity<ActivityPwdBinding>() {
    override fun initBindingVar() {
    }

    override fun initTitle() {
        binding.titlebar.back.setOnClickListener { finish() }
    }

    lateinit var mCountDownTimerUtils: CountDownTimerUtils
    lateinit var context: Context
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initBinding(R.layout.activity_pwd)
        context = this
        initView()
    }


    private fun initView() {

        var type = this.intent.getStringExtra("type").toInt()
        when (type) {
            0 -> {
                binding.titlebar.title.text = "找回密码"
                binding.findSubmit.setOnClickListener {
                    isNull()
                }
            }
            1 -> {
                binding.titlebar.title.text = "绑定手机号"
                binding.findSubmit.text = "确认绑定"
                binding.linNewPwd.visibility = View.GONE
            }
        }

        binding.fotSendCode.setOnClickListener {
            sendCodeHttp()
        }


    }


    private fun isNull() {
        var msg = ""
        var isNull = true
        if (!Constants.validMobile(binding.pwdPhone.text.toString().trim())) {
            msg = "请输入正确的手机号"
            isNull = false
        }

        if (TextUtils.isEmpty(binding.fotCode.text.toString().trim())) {
            msg = "请输入验证码"
            isNull = false
        }

        if (TextUtils.isEmpty(binding.fotNewPwd.text.toString().trim())) {
            msg = "请输入新密码"
            isNull = false
        }

        if (isNull) {
            pwdHttp()
        } else {
            showTips(msg)
            return
        }

    }

    /**
     * 发送验证码
     * */
    private fun sendCodeHttp() {
        if (!Constants.validMobile(binding.pwdPhone.text.toString().trim())) {
            showTips("请输入正确手机号")
            return
        }
        ApiUtill.getInstance().getApi(AppRequest.getAPI().sendCode(binding.pwdPhone.text.toString().trim(), "1"), { baseResponse ->
            baseResponse as TestResponse
            if (baseResponse.code == 200) {
                showTips("验证码已发生到您的手机,请注意查收")
                mCountDownTimerUtils = CountDownTimerUtils(binding.fotSendCode, 60000, 1000)
                mCountDownTimerUtils.start()
            } else {
                showTips("验证码发送失败，请重新发送")
            }
        })
    }

    /**
     * 找回密码
     * */
    private fun pwdHttp() {
        ApiUtill.getInstance().getApi(AppRequest.getAPI().pwd(binding.fotCode.text.toString().trim(), binding.pwdPhone.text.toString().trim(), binding.fotNewPwd.text.toString().trim(), "2"), { baseResponse ->
            baseResponse as TestResponse
            if (baseResponse.code == 200) {
                showTips("修改密码成功")
                finish()
            } else {
                showTips("请求失败，请重新请求！")
            }
        })

    }


}
